/*
 * ShopCategoryFragment.java
 * business
 *
 * Created by ChenTao on 2016/10/25.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.shopdetail.category;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.yidiandao.business.R;
import com.yidiandao.business.base.BaseFragment;
import com.yidiandao.business.constant.Intents;
import com.yidiandao.business.data.bean.Tag;
import com.yidiandao.business.data.event.RefreshShopCategoryStateEvent;
import com.yidiandao.business.data.response.QueryShopCategoryResponse;
import com.yidiandao.business.ui.shopdetail.categorygoods.ShopCategoryGoodsActivity;
import com.yidiandao.business.ui.widget.recyclerview.provider.ShopCategoryProvider;
import com.yidiandao.business.ui.widget.view.TagFlowLayout;
import com.yidiandao.business.ui.widget.view.TagView;
import com.yidiandao.business.utils.UIUtils;
import com.yidiandao.business.utils.UmengUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import me.drakeet.multitype.MultiTypeAdapter;

/**
 * 店铺分类 - View
 */
public class ShopCategoryFragment extends BaseFragment implements ShopCategoryContract.View {

    private ShopCategoryContract.Presenter mPresenter;

    private RecyclerView mRecyclerView;

    private TagFlowLayout mTagFlowLayout;

    private MultiTypeAdapter mCategoryAdapter;

    public static ShopCategoryFragment getInstance(String supplierId) {
        ShopCategoryFragment fragment = new ShopCategoryFragment();
        Bundle bundle = new Bundle();
        bundle.putString(Intents.EXTRA_SUPPLIER_ID, supplierId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void setPresenter(ShopCategoryContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return ShopCategoryFragment.class.getSimpleName();
    }

    @Override
    public void showToast(String text) {
        UIUtils.showToast(text);
    }

    @Override
    protected void initViews() {
        mRecyclerView = (RecyclerView) mRootView.findViewById(R.id.recycler_view);
        mTagFlowLayout = (TagFlowLayout) mRootView.findViewById(R.id.tag_flow_layout);

        initRecyclerView();

        mPresenter.bundleSupplierId(getArguments());
        mPresenter.start();
    }

    private void initRecyclerView() {
        mCategoryAdapter = new MultiTypeAdapter();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));

        mCategoryAdapter.register(
                QueryShopCategoryResponse.ShopCategory.class, new ShopCategoryProvider());

        mRecyclerView.setAdapter(mCategoryAdapter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_shop_category;
    }

    @Override
    public void setCategoryData(List<QueryShopCategoryResponse.ShopCategory> categoryData) {
        mCategoryAdapter.setItems(categoryData);
        mCategoryAdapter.notifyDataSetChanged();
    }

    @Override
    public void setTagLayoutData(final List<Tag> tags) {
        mTagFlowLayout.setTagState(TagView.DISPLAY);
        mTagFlowLayout.setTagsData(tags);

        mTagFlowLayout.setTagListener(
                new TagFlowLayout.TagListener() {
                    @Override
                    public void onTagClick(TagView tagView) {
                        // 点击标签去店铺分类页面
                        mPresenter.toShopCategoryGoodsActivity(tagView);
                    }

                    @Override
                    public void onTextChanged(String text) {
                    }
                });
    }

    @Override
    public void toShopCategoryGoodsActivity(
            String categoryName, String supplierId, String categoryId) {
        // 去店铺分类商品列表
        ShopCategoryGoodsActivity.actionStart(mActivity, categoryName, supplierId, categoryId);
    }

    @Override
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(RefreshShopCategoryStateEvent event) {
        mPresenter.onCategoryItemClick(event.getPosition());
    }

    @Override
    public void notifyDataSetChanged() {
        mCategoryAdapter.notifyDataSetChanged();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public void onResume() {
        super.onResume();
        UmengUtils.onEvent(mActivity, R.string.umeng_shop_detail_categray);
    }
}
